package com.yj.shipxy.service;

import com.by_syk.graphiccr.core.GraphicCTranslator;
import com.yj.util.FileUtil;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;


import java.io.File;
import java.io.IOException;

/**
 * Created by zhengguanyong on 2017/12/28.
 */
//@Service
public class TrainTrackService {
    public static void main(String[] args) {
        String cookieStr = "JSESSIONID=F38FD3439989CFF04490800609E1DA9A; JSESSIONID=4224AD737D3167CBCD1F3357FB0718C8; lyIH6X7dHG=MDAwM2IyYWM3NDQwMDAwMDAwMjUwWmNXHm0xNTE0NDc3MjQw";
        boolean do_while = true;
        while (do_while) {
            String graphCode = getGraphCode(cookieStr);
            String strResult = getTrainPosition(graphCode,cookieStr);
            JSONObject jsonResult = JSONObject.fromObject(strResult);
            if ((boolean)jsonResult.get("success")) {
                do_while = false;
            }
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
    }

    public static String getGraphCode(String cookieStr) {
        String code_url = "http://hyfw.95306.cn/gateway/hywx/TrainWebClient/page/origin/check_code.jsp";
        String suffix = "?_=";
        long timeMillis = System.currentTimeMillis();
        String request_url = code_url+suffix+timeMillis;
        File codeFile = new File("/Users/zhengguanyong/Downloads/jpgcode/getpcode/"+timeMillis+".jpg");
        FileUtil.downloadFile(request_url,codeFile,cookieStr);

        String result = GraphicCTranslator.translate(codeFile, GraphicCTranslator.TYPE_1);
        System.out.println(result);
        return result;
    }

    public static String getTrainPosition(String graphCode,String cookieStr) {
        String result = "";
        HttpClient httpClient = new HttpClient();
        String url = "http://hyfw.95306.cn/gateway/hywx/TrainWebClient/hwzz_uouii.action";
        PostMethod postMethod = new PostMethod(url);
        //设置head
        postMethod.setRequestHeader("Cookie",cookieStr);
//   填入各个表单域的值
        NameValuePair[] data = {
                new NameValuePair("hwzz.yzm", "63FD155B6A364CB4BC1680C1F74B4B37"),
                new NameValuePair("hwzz.type", "1"),
                new NameValuePair("hwzz.carNo", "4881112"),
                new NameValuePair("hwzz.hph", ""),
                new NameValuePair("check_code", graphCode)
        };
//   将表单的值放入postMethod中
        postMethod.setRequestBody(data);
//   执行postMethod
        int statusCode = 0;
        try {
            statusCode = httpClient.executeMethod(postMethod);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//   HttpClient对于要求接受后继服务的请求，象POST和PUT等不能自动处理转发
//   301或者302
        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
            //   从头中取出转向的地址
            Header locationHeader = postMethod.getResponseHeader("location");
            String location = null;
            if (locationHeader != null) {
                location = locationHeader.getValue();
                System.out.println("The page was redirected to:" + location);
            }
            else {
                System.err.println("Location field value is null.");
            }
            return "";
        } else {
            System.out.println("-----------");
            System.out.println(postMethod.getStatusLine());
            String str = "";
            try {
                str = postMethod.getResponseBodyAsString();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            result = str;
            System.out.println(str);
        }
        postMethod.releaseConnection();
        return result;
    }
}
